package com.zhongzhi.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.zhongzhi.bean.Configuration;
import com.zhongzhi.common.Common;

import javafx.stage.Stage;
import lombok.extern.log4j.Log4j;
/**
 * 读取配置系统参数
 * @author Administrator
 *
 */
@Log4j
public class ConfigUtil {
	//设置参数2
	public void  setConfiguration(String computers,String serverIp,String serverProt,String scope1,
			String scope2,String scope3,String scope4,String scope5,String scope6,
			String mod1,String mod2,String mod3,String mod4,String mod5,String mod6,String mod7) {
		
		log.info("设置全局参数");
		FileOutputStream outputStream;
		try {
			String path = getPath();
			System.out.println(path);
			Properties properties = new Properties();
			String setProperties = setProperties();
				outputStream = new FileOutputStream(setProperties);
				
				properties.setProperty("computers", computers.trim());
				properties.setProperty("serverIp", serverIp.trim());
				properties.setProperty("serverProt", serverProt.trim());
				
				//2021/2/25
				properties.setProperty("scope1", scope1.trim());
				properties.setProperty("scope2", scope2.trim());
				properties.setProperty("scope3", scope3.trim());
				properties.setProperty("scope4", scope4.trim());
				properties.setProperty("scope5", scope5.trim());
				properties.setProperty("scope6", scope6.trim());
				properties.setProperty("mod1", mod1.trim());
				properties.setProperty("mod2", mod2.trim());
				properties.setProperty("mod3", mod3.trim());
				properties.setProperty("mod4", mod4.trim());
				properties.setProperty("mod5", mod5.trim());
				properties.setProperty("mod6", mod6.trim());
				properties.setProperty("mod7", mod7.trim());
				
				properties.store(outputStream, "");
				Configuration configuration = Common.getConfiguration();
				configuration.setProperty(computers.trim());
				configuration.setServerId(serverIp.trim());
				configuration.setServerProt(serverProt.trim());
				
				log.info("设置全局参数:computers="+computers.trim()+"  serverIp="+serverIp.trim()+"  ServerProt="+serverProt.trim());
		} catch (Exception e) {
			log.error("设置全局参数失败:"+e);
		}
		
	}
	
	
	//设置参数
	public void  setConfiguration(String computers,String serverIp,String serverProt) {
		log.info("设置全局参数");
		FileOutputStream outputStream;
		try {
			String path = getPath();
			System.out.println(path);
			Properties properties = new Properties();
			String setProperties = setProperties();
				outputStream = new FileOutputStream(setProperties);
				
				properties.setProperty("computers", computers.trim());
				properties.setProperty("serverIp", serverIp.trim());
				properties.setProperty("serverProt", serverProt.trim());
				
				properties.store(outputStream, "");
				Configuration configuration = Common.getConfiguration();
				configuration.setProperty(computers.trim());
				configuration.setServerId(serverIp.trim());
				configuration.setServerProt(serverProt.trim());
				
				log.info("设置全局参数:computers="+computers.trim()+"  serverIp="+serverIp.trim()+"  ServerProt="+serverProt.trim());
		} catch (Exception e) {
			log.error("设置全局参数失败:"+e);
		}
	}
	//读取参数
	public void getConfiguration() {
		log.info("读取全局参数");
		Properties properties =  new Properties();
		try {
			String setProperties = setProperties();
			FileInputStream inputStream = new FileInputStream(setProperties);
			properties.load(inputStream);
			inputStream.close();
			Configuration configuration = Common.getConfiguration();
			configuration.setProperty(properties.getProperty("computers"));
			configuration.setServerId(properties.getProperty("serverIp"));
			configuration.setServerProt(properties.getProperty("serverProt"));
			
			//2021/2/25
			configuration.setScope1(val(properties.getProperty("scope1")));
			configuration.setScope2(val(properties.getProperty("scope2")));
			configuration.setScope3(val(properties.getProperty("scope3")));
			configuration.setScope4(val(properties.getProperty("scope4")));
			configuration.setScope5(val(properties.getProperty("scope5")));
			configuration.setScope6(val(properties.getProperty("scope6")));
			configuration.setMod1(val(properties.getProperty("mod1")));
			configuration.setMod2(val(properties.getProperty("mod2")));
			configuration.setMod3(val(properties.getProperty("mod3")));
			configuration.setMod4(val(properties.getProperty("mod4")));
			configuration.setMod5(val(properties.getProperty("mod5")));
			configuration.setMod6(val(properties.getProperty("mod6")));
			configuration.setMod7(val(properties.getProperty("mod7")));
			
			
			log.info("读取全局参数成功:"+configuration);
		} catch (Exception e1) {
			log.error("读取全局失败"+e1);
		}
	}
	public String setProperties() {
		String path = getPath();
		path = path + "/parameter";
		File file = new File(path);
		if (file.exists()==false) {
			file.mkdirs();
			log.info("检测不到配置文件所在目录,现在已经创建:"+path);
		}
		path = path +"/parameter.properties";
		File file2 = new File(path);
		if (file2.exists()==false) {
			try {
				file2.createNewFile();
				log.info("检测不到配置文件,现在已经创建:"+path);
			} catch (IOException e) {
				log.error("创建文件失败:"+e);
				e.printStackTrace();
			}
		}
		return path;
	}
	
	
	//获取当前jar包路径
	public String getPath()
	{
		return System.getProperty("user.dir");
	}
	
	private double val(String s) {
		try {
			return Double.valueOf(s);
		} catch (Exception e) {
			return 0;
		}
	}

}
